<template>
  <!-- 中铁2021年向审计署报送报表情况统计 -->
  <j-modal
    :title="'报表上报'"
    :width="800"
    :visible="visible"
    :maskClosable="false"
    :ok-button-props="{ props: { disabled: disableSubmit } }"
    :fullscreen="true"
    @close="close"
    cancelText="关闭"
  >
    <template slot="footer">
      <a-button key="back" style="padding-left: 8px; padding-right: 8px" @click="close(false)"> 关闭 </a-button>
      <a-popconfirm :disabled="disableSubmit" title="确定保存吗?" @confirm="() => saveOrSubmit('save', null)">
        <a-button key="save" style="padding-left: 8px; padding-right: 8px; margin-left: 8px" :disabled="disableSubmit">
          保存
        </a-button>
      </a-popconfirm>
      <a-button
        key="submit"
        type="primary"
        style="padding-left: 8px; padding-right: 8px; margin-left: 8px"
        :disabled="disableSubmit"
        @click="close(true)"
      >
        提交审批
      </a-button>
    </template>
    <a-spin :spinning="Loading">
      <div :class="{ isCheck: isCheck }">
        <!-- <div class="table-header">
          <span class="table-title">填报单位：</span>{{ model.departName }} <span class="table-title">审计负责人：</span
          >{{ model.auditPersonName }} <span class="table-title">填表人：</span>{{ model.createByText }}
          <span class="table-title">填表时间：</span>{{ model.createTime }} <span class="table-title">统计时间：</span
          >{{ model.year }}年（{{ model.quarter }}季度）
        </div> -->
        <a-table
          class="edit-list"
          ref="table"
          size="middle"
          bordered
           rowKey="id"
          width="100vw"
          :pagination="false"
          :columns="columns"
          :dataSource="dataSource"
          :scroll="{ y: 520 }"
        >
          <div slot="title">
            <div class="title">{{ title }}</div>
            <div class="sub-title">
              <div>填报单位：{{ model.departName }}</div>
              <div>{{ model.year }}年（{{ model.quarter }}季）度</div>
              <!-- <div>审计负责人：{{ model.auditPersonName }}</div>
              <div>填表人：{{ model.createByText }}</div>
              <div>填表时间：{{ model.createTime }}</div>
              <div>金额单位（万元）</div> -->
            </div>
          </div>
          <template v-slot:departName="text, record">
            <div
              v-if="disableSubmit || text == '合计'"
              style="width: 100%; height: 45px; overflow-x: hidden; overflow-y: auto"
            >
              {{ record.departName }}
            </div>
            <a-input v-else class="noborderInput" :disabled="disableSubmit" v-model="record.departName" />
          </template>
          <template v-slot:jewtzgl="text, record">
            <a-input v-if="!disableSubmit" class="noborderInput" :disabled="disableSubmit" v-model="record.jewtzgl" />
            <span v-else>{{ record.jewtzgl }}</span>
          </template>
          <template v-slot:fjewtzgl="text, record">
            <a-input v-if="!disableSubmit" class="noborderInput" :disabled="disableSubmit" v-model="record.fjewtzgl" />
            <span v-else>{{ record.fjewtzgl }}</span>
          </template>
          <template v-for="(col, index) in colrender" v-slot:[col]="text, record">
            <span v-if="record.departName == '合计'" class="spancell" :key="col">{{ text }}</span>
            <a-input-number
              v-else-if="!disableSubmit"
              class="inputnumber"
              v-model="record[col]"
              :disabled="disableSubmit"
              :precision="0"
              :key="index"
              @change="handleChange(record, col)"
            />
            <span :key="index" v-else>{{ record[col] }}</span>
          </template>
        </a-table>
      </div>
    </a-spin>
    <SelectApprove ref="approver" @submitData="saveOrSubmit"></SelectApprove>
  </j-modal>
</template>

<script>
import SelectApprove from '../SelectApprover'
import { httpAction, getAction, postAction, deleteAction } from '@/api/manage'
import columns from './columns9'
import store from '@/store/'
import pubMothod from './PublicMethod'
export default {
  name: 'ModelView9',
  components: {
    SelectApprove,
  },
  data() {
    return {
      type: '7',
      sjType: '7',
      state: '1',
      title: '',
      columns: columns(),
      colrender: ['bsbbsl', 'wnrtjfwdw', 'jcxsw', 'qlzxdw', 'ytrmlkpzdw'],
      userInfo: store.getters.userInfo,
      departInfo: store.getters.departInfo,
      disableSubmit: false,
      dataSource: [],
      title: '',
      model: {},
      visible: false,
      isCheck: false,
      officeId: '',
      btnStyle: 'save',
      approveId: '',
      Loading: false,
      url: {
        //保存或编辑
        list: '/sjxxzhbb/sjsbb/queryBsbbqktjInfoById',
        save: '/sjxxzhbb/sjsbb/saveBsbbqktjInfo',
        // 提交审批
        submitdata: '/common/sjProcCommon/bsbbqktjShen',
        delete: '/sjxxzhbb/sjAuditInfo/remove',
      },
    }
  },
  methods: {
    add(record) {
      this.model = Object.assign({}, record)
      this.visible = true
      this.setReportTitle()
      this.model.fileId = ''
      this.dataSource = [
        {
          departName: '合计',
          bsbbsl: 0,
          wnrtjfwdw: 0,
          jcxsw: 0,
          qlzxdw: 0,
          ytrmlkpzdw: 0,
          jewtzgl: '',
          fjewtzgl: '',
        },
        {
          departName: this.model.departName,
          bsbbsl: 0,
          wnrtjfwdw: 0,
          jcxsw: 0,
          qlzxdw: 0,
          ytrmlkpzdw: 0,
          jewtzgl: '',
          fjewtzgl: '',
        },
      ]
    },
    check(record) {
      this.model.id = record.id
      this.visible = true
      this.getTableData()
    },
    edit(record) {
      this.model.id = record.id
      this.visible = true
      this.getTableData()
    },
    getTableData() {
      this.Loading = true
      getAction(this.url.list, {
        id: this.model.id,
        sjType: this.sjType,
      }).then((res) => {
        if (res.success && res.result.length > 0) {
          this.dataSource = res.result[0].infoEntity || []
          this.model.createTime = res.result[0].createTime
          this.model.createByText = res.result[0].createByText
          this.model.auditPersonName = res.result[0].headName
          this.model.createBy = res.result[0].createBy
          this.model.departName = res.result[0].departName
          this.model.year = res.result[0].year
          this.model.quarter = res.result[0].quarter
          this.model.fileId = res.result[0].fileId
        }
        this.Loading = false
      })
    },
    setReportTitle() {
      let nowDate = new Date()
      this.model.createTime = pubMothod.dateFormat('yyyy-MM-dd hh:mm:ss', nowDate)
      this.model.addtime = parseInt(Number(nowDate.getTime()) / 1000)
      this.model.createByText = this.userInfo.realname
      this.model.auditPersonName = this.userInfo.realname
      this.model.departName = this.departInfo.departName
      this.model.createBy = this.userInfo.id
      this.model.shenRespId = this.userInfo.id
      this.model.officeId = this.departInfo.id
    },
    handleChange(record, col) {
      if (record[col] == null || record[col] == undefined) record[col] = 0
      let sum = 0
      this.dataSource.map((item) => {
        if (!(item.departName == '合计')) {
          sum += parseFloat(item[col] || '0')
        }
      })
      this.dataSource[0][col] = sum
    },
    saveOrSubmit(style, approveId) {
      this.btnStyle = style
      this.approveId = approveId
      // 提交并审批
      this.saveData()
    },
    creatReport() {
      getAction(this.url.getCreatData, { id: '' }).then((res) => {
        if (res.success && res.result[0]) {
          this.dataSource = JSON.parse(res.result[0].reportInfo)
          this.Loading = false
        } else {
        }
      })
    },
    saveData() {
      this.Loading = true
      let param = {
        id: this.model.id,
        year: this.model.year,
        sjType: this.sjType,
        quarter: this.model.quarter,
        info: JSON.stringify(this.dataSource),
        head: this.model.createBy,
      }
      postAction(this.url.save, param).then((res) => {
        this.Loading = false
        if (res.success) {
          if (!this.model.id) {
            this.model.id = res.result
          }
          if (this.btnStyle == 'save') {
            this.$message.success(res.message)
            this.visible = false
            this.$emit('ok')
            return
          } else {
            this.submitData(this.approveId)
          }
        } else {
          this.$message.error(res.message)
        }
      })
    },
    // 提交数据
    submitData(userId) {
      let params = {
        proId: this.model.id,
        userIds: userId,
        type: this.type,
        state: this.state,
        sjType: this.sjType,
      }
      getAction(this.url.submitdata, params).then((res) => {
        this.Loading = false
        if (res.success) {
          this.$message.success(res.message)
          this.visible = false
          this.$refs.approver.confirmLoading = false
          this.$emit('ok')
        } else {
          console.log(res)
        }
      })
    },
    close(isSubmit) {
      if (isSubmit) {
        this.$refs.approver.showApproveConfirm = true
      } else {
        this.dataSource = []
        this.visible = false
      }
    },
    delete(record) {
      deleteAction(this.url.delete, { id: record.id }).then((res) => {
        if (res.success) {
          this.$message.success(res.message)
          this.$emit('ok')
        } else {
          this.$message.error(res.message)
        }
      })
    },
  },
}
</script>

<style lang="less" scoped>
// @import '../../../css/checkAndEdit.less';
@import '../../../../css/baobiaotable.less';

.table-header {
  margin-bottom: 10px;

  .table-title {
    font-weight: bold;
    margin-left: 15px;
  }
}
</style>